package com.cis.web.controller.education;

import java.util.ArrayList;
import java.util.List;

import com.cis.education.domain.EduCheckinStudent;
import com.cis.education.domain.EduStudent;
import com.cis.education.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cis.common.annotation.Log;
import com.cis.common.enums.BusinessType;
import com.cis.education.domain.EduCheckin;
import com.cis.common.core.controller.BaseController;
import com.cis.common.core.domain.AjaxResult;
import com.cis.common.utils.poi.ExcelUtil;
import com.cis.common.core.page.TableDataInfo;

/**
 * 签到管理Controller
 * 
 * @author 管理员
 * @date 2022-01-19
 */
@Controller
@RequestMapping("/education/checkin")
public class EduCheckinController extends BaseController
{
    private String prefix = "education/checkin";

    @Autowired
    private IEduCheckinService eduCheckinService;

    @Autowired
    private IEduTeacherService eduTeacherService;

    @Autowired
    private IEduCourseService eduCourseService;

    @Autowired
    private IEduClassroomService eduClassroomService;

    @Autowired
    private IEduStudentService eduStudentService;

    @Autowired
    private IEduCheckinStudentService eduCheckinStudentService;

    @RequiresPermissions("education:checkin:view")
    @GetMapping()
    public String checkin()
    {
        return prefix + "/checkin";
    }

    /**
     * 查询签到管理列表
     */
    @RequiresPermissions("education:checkin:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EduCheckin eduCheckin)
    {
        startPage();
        List<EduCheckin> list = eduCheckinService.selectEduCheckinList(eduCheckin);
        for (EduCheckin checkin : list) {
            checkin.setTeacher(eduTeacherService.selectEduTeacherById(checkin.getTeacherId()));
            checkin.setClassroom(eduClassroomService.selectEduClassroomById(checkin.getClassroomId()));
            checkin.setCourse(eduCourseService.selectEduCourseById(checkin.getCourseId()));
        }
        return getDataTable(list);
    }

    /**
     * 查询签到详细信息
     */
    @GetMapping("/detail/{checkinId}")
    public String detail(@PathVariable Integer checkinId, ModelMap mmap){
        EduCheckin eduCheckin = eduCheckinService.selectEduCheckinById(checkinId);


        EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
        eduCheckinStudent.setCheckinId(eduCheckin.getId());
        List<EduCheckinStudent> eduCheckinStudents = eduCheckinStudentService.selectEduCheckinStudentList(eduCheckinStudent);
        eduCheckin.setTotalNum(eduCheckinStudents.size());
        eduCheckinStudent.setStatus(1);
        List<EduCheckinStudent> attendStudents = eduCheckinStudentService.selectEduCheckinStudentList(eduCheckinStudent);
        eduCheckin.setAttendNum(attendStudents.size());

        mmap.put("checkin",eduCheckin);
        mmap.put("teacher", eduTeacherService.selectEduTeacherById(eduCheckin.getTeacherId()));
        mmap.put("course", eduCourseService.selectEduCourseById(eduCheckin.getCourseId()));
        mmap.put("classroom", eduClassroomService.selectEduClassroomById(eduCheckin.getClassroomId()));
        return prefix + "/detail";
    }

    @PostMapping("/{id}/student/list")
    @ResponseBody
    public TableDataInfo studentList(@PathVariable Integer id){
        EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
        eduCheckinStudent.setCheckinId(id);
        List<EduCheckinStudent> eduCheckinStudents = eduCheckinStudentService.selectEduCheckinStudentList(eduCheckinStudent);
        List<EduStudent> studentList = new ArrayList<>();
        for (EduCheckinStudent checkinStudent : eduCheckinStudents) {
            EduStudent eduStudent = eduStudentService.selectEduStudentById(checkinStudent.getStudentId());
            eduStudent.setCheckinStatus(checkinStudent.getStatus());
            studentList.add(eduStudent);
        }
        return getDataTable(studentList);
    }

    /**
     * 导出签到管理列表
     */
    @RequiresPermissions("education:checkin:export")
    @Log(title = "签到管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EduCheckin eduCheckin)
    {
        List<EduCheckin> list = eduCheckinService.selectEduCheckinList(eduCheckin);
        ExcelUtil<EduCheckin> util = new ExcelUtil<EduCheckin>(EduCheckin.class);
        return util.exportExcel(list, "签到管理数据");
    }

    /**
     * 新增签到管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存签到管理
     */
    @RequiresPermissions("education:checkin:add")
    @Log(title = "签到管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EduCheckin eduCheckin)
    {
        return toAjax(eduCheckinService.insertEduCheckin(eduCheckin));
    }

    /**
     * 修改签到管理
     */
    @RequiresPermissions("education:checkin:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        EduCheckin eduCheckin = eduCheckinService.selectEduCheckinById(id);
        mmap.put("eduCheckin", eduCheckin);
        return prefix + "/edit";
    }

    /**
     * 修改保存签到管理
     */
    @RequiresPermissions("education:checkin:edit")
    @Log(title = "签到管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EduCheckin eduCheckin)
    {
        return toAjax(eduCheckinService.updateEduCheckin(eduCheckin));
    }


    /**
     * 删除签到管理
     */
    @RequiresPermissions("education:checkin:remove")
    @Log(title = "签到管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(eduCheckinService.deleteEduCheckinByIds(ids));
    }
}
